package com.powerhua.workflow.jbpm.deploy.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.zip.ZipInputStream;

import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.graph.def.ProcessDefinition;

import com.powerhua.workflow.jbpm.deploy.JbpmDeploy;
import java.io.InputStream;

public class JbpmDeployImpl implements JbpmDeploy {

    @Override
    public String deploy(InputStream in) {
        StringBuffer result = new StringBuffer("<def>");
        JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
        JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
        try {
            ZipInputStream zipInputStream = new ZipInputStream(in);
            ProcessDefinition processDefinition = ProcessDefinition.parseParZipInputStream(zipInputStream);
            jbpmContext.deployProcessDefinition(processDefinition);
            
            result.append("<def-id>" + processDefinition.getId() + "</def-id>");
            result.append("<def-name>" + processDefinition.getName() + "</def-name>");
            result.append("<def-version>" + processDefinition.getVersion() + "</def-version>");
            
            zipInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            jbpmContext.close();
        }
        
        result.append("</def>");
        return result.toString();

    }
}
